package samfeng.erp.warehouse.service;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
//import cn.iocoder.yudao.module.stock.controller.admin.warehouse.vo.*;
//import cn.iocoder.yudao.module.stock.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import samfeng.erp.warehouse.controller.vo.WarehouseCreateReqVO;
import samfeng.erp.warehouse.controller.vo.WarehouseExportReqVO;
import samfeng.erp.warehouse.controller.vo.WarehousePageReqVO;
import samfeng.erp.warehouse.controller.vo.WarehouseUpdateReqVO;
import samfeng.erp.warehouse.convert.WarehouseConvert;
import samfeng.erp.warehouse.dal.dataobject.WarehouseDO;
import samfeng.erp.warehouse.dal.mysql.WarehouseMapper;

//import cn.iocoder.yudao.module.stock.convert.warehouse.WarehouseConvert;
//import cn.iocoder.yudao.module.stock.dal.mysql.warehouse.WarehouseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static samfeng.erp.warehouse.enmus.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;
//import static cn.iocoder.yudao.module.stock.enums.ErrorCodeConstants.*;

/**
 * 仓库 Service 实现类
 *
 * @author samfeng
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public Long createWarehouse(WarehouseCreateReqVO createReqVO) {
        // 插入
        WarehouseDO warehouse = WarehouseConvert.INSTANCE.convert(createReqVO);
        warehouseMapper.insert(warehouse);
        // 返回
        return warehouse.getId();
    }

    @Override
    public void updateWarehouse(WarehouseUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWarehouseExists(updateReqVO.getId());
        // 更新
        WarehouseDO updateObj = WarehouseConvert.INSTANCE.convert(updateReqVO);
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(Long id) {
        // 校验存在
        this.validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }

    private void validateWarehouseExists(Long id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public List<WarehouseDO> getWarehouseList(Collection<Long> ids) {
        return warehouseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseDO> getWarehouseList(WarehouseExportReqVO exportReqVO) {
        return warehouseMapper.selectList(exportReqVO);
    }

}
